package com.lsl.test1;

/**
 * @author lishuailong
 * @create 2020-07-03 17:27
 */
public class 线程的死锁 {
    public static void main(String[] args) {
        StringBuffer s1 = new StringBuffer();
        StringBuffer s2 = new StringBuffer();

        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (s1){
                    s1.append("1");
                    s2.append("a");
                    synchronized (s2){
                        s1.append("2");
                        s2.append("b");
                        System.out.println(s1);
                        System.out.println(s2);
                    }
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (s2){
                    s1.append("3");
                    s2.append("c");

                    synchronized (s1){
                        s1.append("4");
                        s2.append("d");
                        System.out.println(s1);
                        System.out.println(s2);
                    }
                }
            }
        }).start();

    }
}


